- 
                Notifications
    
You must be signed in to change notification settings  - Fork 2.2k
 
          fix: handle null py::handle and add tests for py::scoped_critical_section
          #5706
        
          New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
  
    fix: handle null py::handle and add tests for py::scoped_critical_section
  
  #5706
              Conversation
| 
           Add a note to: https://peps.python.org/pep-0703/#python-critical-sections  | 
    
| 
           @XuehaiPan Could you please add tests?  | 
    
py::handle for py::scoped_crititcal_sectionpy::handle for py::scoped_critical_section
      | 
           What did you have in mind for tests? It's not that easy to test that this is or is not locking. You could just make sure it was callable with nullptr (since that does nothing), I suppose?  | 
    
8828478    to
    032fef2      
    Compare
  
    
          
 Sorry I somehow missed this question before. Just covering all situations with  Basic code coverage. Currently we have only this:  | 
    
| 
           I forgot to mention: I'm wondering, how is   | 
    
58763fb    to
    4725667      
    Compare
  
    | 
           Looks like C++20+ has a problem.  | 
    
| 
           Do you think you can solve the segfaults for C++20 and 23?  | 
    
      
        
              This comment was marked as outdated.
        
        
      
    
  This comment was marked as outdated.
d6084c0    to
    fcfcddb      
    Compare
  
    | 
           The test failure on macOS with Python 3.14t seems unrelated and I cannot reproduce locally.  | 
    
fcfcddb    to
    950c15c      
    Compare
  
    py::handle for py::scoped_critical_sectionpy::handle and add tests for py::scoped_critical_section
      cd724f0    to
    009756f      
    Compare
  
    | 
           Drive-by comment. The new test is much more comprehensive than I was hoping! I'll need to find some time to look carefully. (Hopefully later today.)  | 
    
| 
           I don't think it's valid in the free-threading build to call gc.collect and expect things to be collected. Collections happen as part of "stop-the-world" events, which that's not going to trigger, as it needs to sync the dual ref-counts. I think that's why the 3.14t test is failing. Generally gc-based tests are not very good, there really aren't strong language-level guarantees about things getting cleaned up, it's implementation based.  | 
    
… guard against name clashes).
| namespace test_scoped_critical_section_ns { | ||
| 
               | 
          ||
| void test_one_nullptr() { py::scoped_critical_section lock{py::handle{}}; } | ||
| 
               | 
          ||
| void test_two_nullptrs() { py::scoped_critical_section lock{py::handle{}, py::handle{}}; } | ||
| 
               | 
          ||
| void test_first_nullptr() { | ||
| py::dict d; | ||
| py::scoped_critical_section lock{py::handle{}, d}; | ||
| } | ||
| 
               | 
          ||
| void test_second_nullptr() { | ||
| py::dict d; | ||
| py::scoped_critical_section lock{d, py::handle{}}; | ||
| } | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I was suggesting to add tests, all I had in mind was something simple like this.
The additional tests look interesting though.
Could you please add comments to explain what the tests are for? (I'd ask my favorite LLM for suggestions, usually it's really quick that way.)
| 
           This crossed my mind: If it's troublesome to get the new comprehensive tests to work, I'd be in favor of splitting this PR, to just keep the changes in   | 
    
| 
           Everything passes, awesome! Could you please review this analysis and distill out source code comments suitable for adding to this PR? https://chatgpt.com/share/683fea11-df78-8008-b76d-749f9e8f0df8  | 
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! Just one minor suggestion.
@henryiii to you want to include this in RC3? (I think that could be useful.)
Signed-off-by: Henry Schreiner <[email protected]>
py::handle and add tests for py::scoped_critical_sectionpy::handle and add tests for py::scoped_critical_section
      
Description
A minor improvement to check if the handle is null.
Suggested changelog entry:
py::handleforpy::scoped_crititcal_sectionpy::scoped_crititcal_section